<?php

namespace app\admin\model;

use app\common\model\TimeModel;

class Order extends TimeModel {

    /*
     * 托管
     */
    const ORDER_TG = 1;
    /*
     * 矿机
     */
    const ORDER_KJ = 2;
    /*
     * 算力
     */
    const ORDER_SL = 3;
    /*
     * 质押
     */
    const ORDER_ZY = 4;
    /*
     * 存币
     */
    const ORDER_CB = 5;
    /*
     * P盘
     */
    const ORDER_PP = 6;

    protected $name = "order";
    protected $deleteTime = "delete_time";

    public function user() {
        return $this->belongsTo('\app\admin\model\User', 'uid', 'id');
    }


    public function getTypeList() {
        return ['1' => '托管', '2' => '矿机', '3' => '算力', '4' => '抵押', '5' => '存币', '6' => 'P盘',];
    }

    public function getGoodsTypeList() {
        return ['1' => '现货', '2' => '期货',];
    }

    public function getStatusList() {
        return [
            '1'  => '未支付',
            '2'  => '待交割',
            '3'  => '已发货',
            '4'  => '运行中',
            '7'  => '已平仓',
            '8'  => '已赎回',
            '9'  => '已完结',
            '10' => '已取消'
        ];
    }

    /**
     *根据TYPE and type_id 获得商品名称
     * @param $type
     * @param $type_id
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/9 上午11:03
     */
    public function orderType($type, $type_id) {
        //        1:托管,2:矿机,3:算力,4:抵押,5:存币,6:P盘
        switch ($type) {
            case self::ORDER_TG:
            case self::ORDER_KJ:
                $model = new MallGoods();
                break;
            case self::ORDER_SL:
                $model = new Calculation();
                break;
        }
        $product = $model->where('id', $type_id)->find();
        if ($product) {
            $product_title = $product->title;
        } else {
            $product_title = "数据已失效";
        }
        return $product_title;
    }

    /**
     *获取矿机 and 托管下单信息
     * @param $all
     * @param $order_data
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/9 上午11:09
     */
    public function getOrderNodeInfo($all, $order_data) {
        $goods       = MallGoods::instance()->where(['id' => $all['type_id'], 'status' => 1])->find()->toArray();
        $goods_norms = MallGoodsNorms::instance()->where([
            'id'       => $all['norms_id'],
            'goods_id' => $all['type_id']
        ])->find()->toArray();
        //判断是否需要托管
        if ($all['type'] == self::ORDER_TG) {
            $order_data['money']        = $goods_norms['norms_price'] * $all['number'];
            $order_data['entrust_days'] = 30;
            $order_data['produce']      = $goods['produce'];
        } else {
            $order_data['money'] = $goods_norms['norms_price'] * $all['number'];
        }
        $order_data['information']  = $goods['title'];
        $order_data['status']       = 2;
        $order_data['days_time']    = $goods['datys'];
        $order_data['produce_type'] = $goods['produce_type'];

        return $order_data;
    }

    /**
     *获取算力下单信息
     * @param $all
     * @param $order_data
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 上午11:54
     */
    public function getOrderPowerInfo($all, $order_data) {
        $goods                      = Calculation::instance()->where([
            'id'     => $all['type_id'],
            'status' => 1
        ])->find()->toArray();
        $order_data['goods_type']   = $goods['type'];
        $order_data['money']        = $goods['payment_price'] * $all['number'] * $goods['calculation_days_id'];
        $order_data['entrust_days'] = $goods['calculation_days_id'];
        $order_data['produce']      = $goods['produce'];
        $order_data['information']  = $goods['title'];
        if ($goods['type'] === 2) {
            $order_data['days_time'] = $goods['datys'];
        }
        $order_data['status']       = 2;
        $order_data['produce_type'] = $goods['produce_type'];

        return $order_data;
    }


}